<template>
<div>
  <el-button type="primary" plain @click="dialogVisible = true;"
             style="position: absolute;left: 330px;top: 80px;">添加</el-button>
  <el-dialog
    title="提示"
    :visible.sync="dialogVisible"
    width="30%">
    <el-input placeholder="请输入作者名" v-model="authoradd.name" style="width: 300px"></el-input><br><br>
    <el-input placeholder="请输入性别" v-model="authoradd.sex" style="width: 300px"></el-input><br><br>
    <el-input placeholder="请输入代表作" v-model="authoradd.magnum" style="width: 300px"></el-input><br><br>
    <el-input placeholder="请输入籍贯" v-model="authoradd.abj" style="width: 300px"></el-input><br><br>
    <el-input placeholder="请输入简介" v-model="authoradd.brief" style="width: 300px"></el-input><br><br>
    <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="add()">确 定</el-button>
  </span>
  </el-dialog>
  <div>
  <br>
  <br>
  <el-table
    :data="tableData"
    style="width: 100%">
    <el-table-column type="expand">
      <template slot-scope="props">
        <el-form label-position="left" inline class="demo-table-expand">
          <el-form-item label="作者简介:">
            <span>{{ props.row.brief }}</span>
          </el-form-item>
        </el-form>
      </template>
    </el-table-column>
    <el-table-column
      label="编号"
      prop="id">
    </el-table-column>
    <el-table-column
      label="作者名"
      prop="name">
    </el-table-column>
    <el-table-column
      label="性别"
      prop="sex">
    </el-table-column>
    <el-table-column
      label="代表作"
      prop="magnum">
    </el-table-column>
    <el-table-column
      label="籍贯"
      prop="abj">
    </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="mini"
          type="primary"
          icon="el-icon-edit"
          @click="dialogFormVisible=true;authorupd=scope.row"></el-button>
        <el-button
          size="mini"
          type="danger" icon="el-icon-delete"
          @click="del(scope.row)"></el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    small
    layout="total,prev, pager, next"
    :page-size="pageSize"
    :total="total" @current-change="page">
  </el-pagination>

    <el-dialog title="纸质修改" :visible.sync="dialogFormVisible">
      <el-form :model="authorupd">
        <el-form-item label="编号">
          <el-input v-model="authorupd.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="名称" >
          <el-input v-model="authorupd.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" >
          <el-input v-model="authorupd.sex" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="代表作" >
          <el-input v-model="authorupd.magnum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="籍贯" >
          <el-input v-model="authorupd.abj" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介" >
          <el-input v-model="authorupd.brief" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="upd()">确 定</el-button>
      </div>
    </el-dialog>

</div>
</div>
</template>

<script>
export default {
  name: "Author",
  data(){
    return{
      total: 0,
      pageSize: 6,
      tableData: [],
      dialogVisible:false,
      dialogFormVisible:false,
      authoradd:{name:"",sex:"",head:"",magnum:"",abj:"",brief:""},
      authorupd:{}

    }
  },
  created() {
    this.find()
  },
  methods:{
    find(){
      this.$http.post("/backservice/author/findpage?pageNum=0&pageSize=" + this.pageSize).then(response => {
        console.log(response.userList)
        this.tableData = response.userList
        this.total = response.TotalNum
      })
    },
    page(currentPage) {
      this.$http.post('/backservice/author/findpage?&pageNum=' + (currentPage - 1) + '&pageSize=' + this.pageSize)
        .then(response => {
          this.tableData = response.userList
        })
    },
    add(){
      this.dialogVisible=false
      this.$http.post("/backservice/author/add",this.authoradd).then(data=>{
        if (data>0){
          this.find()
          alert("添加成功")
        }else {
          alert("添加失败")
        }
      })
    },
    upd(){
      this.dialogFormVisible=false
      this.$http.post("/backservice/author/update",this.authorupd).then(data=>{
        if (data>0){
          this.find()
          alert("修改成功")
        }else {
          alert("修改失败")
        }
      })
    },
    del(row){
      this.$http.post("/backservice/author/delete",{id:row.id}).then(data=>{
        if (data>0){
          this.find()
          alert("删除成功")
        }else {
          alert("删除失败")
        }
      })
    }
  }
}
</script>

<style scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
